<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .todo-container {
      max-width: 600px;
      padding: 20px;
      margin: 20px auto;
      border: 1px solid #666;
      border-radius: 10px;
    }

    .todo-header {
      display: flex;
      padding: 5px;
      margin-bottom: 10px;
    }
    .todo-header input{
      flex: 1;
      height: 50px;
      border-radius: 5px;
    }
    .todo{
      display: flex;
      border: 1px solid #999;
      padding: 10px;
    
    }
    .todo span{
      flex: 1;
    }
    .todo div{
      margin-left: 10px;
    }
    .undone{
      background-color: #7ada75;
    }
    .done{
      background-color: #aaa8a8;
    }
    [v-cloak] {
      display: none;
    }
  </style>
</head>

<body>
  <div id="app">
    <div class="todo-container" v-cloak>
      <div class="todo-header">
        <input type="text" v-model.trim="todoTxt">
        <button @click="addTodo">增加</button>
      </div>
      <div class="todo-content">
        <div class="todo" v-if="todos.length === 0">暂无待办事项</div>
        <div :class="['todo', {undone: !todo.done, done: todo.done}]" v-for="(todo, index) in todos" :key="index">
          <span>
            {{todo.content}}
          </span>
          <div>
            <button @click="delTodo(index)">删除</button>
            <button @click="setTodoDone(index)" :disabled="todo.done">点击已完成</button>
          </div>
        </div>
      </div>
    </div>
  </div>
  <script src="./vue.global.js"></script>
  <script>
    const { createApp } = Vue;
    const app = createApp({
      data() {
        return {
          todoTxt: '',
          todos: [
            {
              content: '待办事项1',
              done: false
            },
            {
              content: '待办事项2',
              done: true
            },
            {
              content: '待办事项3',
              done: false
            }
          ]
        }
      },
      methods: {
        setTodoDone(index){
          // 设置待办事项已完成
          this.todos[index].done = true;
        },
        delTodo(index) {
          // 删除待办事项
          if (confirm('您确认删除吗?')) {
            this.todos.splice(index, 1);
          }
        },
        // 新增代办事项
        addTodo(){
          if (this.todoTxt === '') {
            return false;
          }
          this.todos.push({
            content: this.todoTxt,
            done: false
          })
          this.todoTxt = '';
        }
      },
    })

    app.mount('#app')
  </script>
</body>

</html>